package cn.xioabai.common.utility;

import cn.hutool.core.io.resource.ClassPathResource;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.InputStream;

/**
 * 类加载文件处理
 *
 * @Author yangdaji
 * @Date 2022/10/16
 * @PackageName cn.xioabai.utils
 * @ClassName ClassLoaderPathResourceUtils
 */
public class ClassLoaderPathResourceUtils {

    private static Logger log = LoggerFactory.getLogger(ClassLoaderPathResourceUtils.class);
    /**
     * 获取类路径下的文件输入流
     *
     * @param fileName 文件名称
     * @return
     */
    public static InputStream getClassLoaderInputStream(String fileName) {
        if (StringUtils.isBlank(fileName)) {
            return null;
        }

        ClassPathResource classPathResource = null;
        try {
            classPathResource = new ClassPathResource(fileName);
        } catch (Exception e) {
            log.error("文件异常: {} 请检查文件名称或位置是否正确!", e.getMessage());
            return null;
        }
        return classPathResource.getClassLoader().getResourceAsStream(fileName);
    }

}
